package com.nsgf.library.widget.prckerview;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.JavascriptInterface;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.gson.Gson;
import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.nsgf.library.BR;
import com.nsgf.library.R;
import com.nsgf.library.data.api.APIs;
import com.nsgf.library.data.bean.LibraryInfo;
import com.nsgf.library.databinding.FargmentNsgfWebviewBinding;
import com.nsgf.library.ui.GotoActivity;
import com.nsgf.library.ui.page.BaseFragment;
import com.nsgf.library.ui.page.HomeMoreActivity;
import com.nsgf.library.ui.page.StateHolder;
import com.nsgf.library.ui.page.WebViewActivity;
import com.nsgf.library.ui.page.video.VideoListActivity;

public class WebViewFragment extends BaseFragment {

    private StateHolder mState;

    public static WebViewFragment newInstance(boolean type) {
        WebViewFragment webViewFragment = new WebViewFragment();
        Bundle bundle = new Bundle();
        bundle.putBoolean("type", type);
        webViewFragment.setArguments(bundle);
        return webViewFragment;

    }

    @Override
    protected void initViewModel() {
        mState = getActivityScopeViewModel(StateHolder.class);
    }

    private FargmentNsgfWebviewBinding mBinding;

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        return new DataBindingConfig(R.layout.fargment_nsgf_webview, BR.vm, mState);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mBinding = (FargmentNsgfWebviewBinding) getBinding();
        mBinding.webView.getWebView().addJavascriptInterface(new TestInterface(), "android");

    }

    /**
     * Js调用的JavascriptInterface
     */
    public class TestInterface {


        @JavascriptInterface
        public void callVideo() {
            Intent intent = new Intent(getActivity(), VideoListActivity.class);
            startActivity(intent);
        }
        @JavascriptInterface
        public void callWarning() {
            GotoActivity.gotoWarningActivity(getActivity(), "1");
        }


        @JavascriptInterface
        public void callWater(String title, int type) {
            Intent intent = new Intent(getActivity(), HomeMoreActivity.class);
            intent.putExtra("title", title);
            intent.putExtra("type", type);
            startActivity(intent);
        }


        @JavascriptInterface
        public void callVideo(String src) {
            Intent intent = new Intent(getActivity(), VideoListActivity.class);
            startActivity(intent);
        }

    }

}
